package com.blog4j.server.modules.log.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blog4j.common.constants.CommonConstant;
import com.blog4j.common.enums.OperationTypeEnum;
import com.blog4j.common.model.LogVo;
import com.blog4j.server.modules.log.entity.LogEntity;
import com.blog4j.server.modules.log.mapper.LogMapper;
import com.blog4j.server.modules.log.service.LogService;
import com.blog4j.server.modules.log.vo.req.LogListReqVo;
import com.blog4j.server.modules.log.vo.resp.LogRespVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 98k灬
 * @version v1.0.0
 * @Description : 功能描述
 * @Create on : 2024/8/19 19:54
 **/
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class LogServiceImpl extends ServiceImpl<LogMapper, LogEntity>
        implements LogService {
    @Value("${custom.logEnabled}")
    private boolean logEnabled;

    @Value("${custom.queryLogEnabled}")
    private boolean queryLogEnabled;

    /**
     * 添加日志
     *
     * @param logVo 日志信息
     */
    @Override
    public void addLog(LogVo logVo) {
        if (!logEnabled) {
            return;
        }

        if (StringUtils.equals(logVo.getOperationType(), OperationTypeEnum.QUERY.getType())
                && !queryLogEnabled) {
            return;
        }
        LogEntity logEntity = new LogEntity();
        logEntity.setId(null);
        BeanUtils.copyProperties(logVo, logEntity);
        this.baseMapper.insert(logEntity);
    }

    /**
     * 日志信息分页查询
     *
     * @param reqVo 请求
     * @return 日志信息
     */
    @Override
    public PageInfo<LogRespVo> logList(LogListReqVo reqVo) {
        Integer pageNo = reqVo.getPageNo();
        Integer pageSize = reqVo.getPageSize();
        if (pageSize > CommonConstant.MAX_PAGE_SIZE) {
            pageSize = CommonConstant.MAX_PAGE_SIZE;
        }
        String operationType = reqVo.getOperationType();
        String module = reqVo.getModule();
        String result = reqVo.getResult();
        String startDate = reqVo.getStartDate();
        String endDate = reqVo.getEndDate();

        LambdaQueryWrapper<LogEntity> wrapper = new LambdaQueryWrapper<LogEntity>()
                .orderByDesc(LogEntity::getCreateTime);

        if (StringUtils.isNotBlank(operationType)) {
            wrapper.eq(LogEntity::getOperationType, operationType);
        }

        if (StringUtils.isNotBlank(module)) {
            wrapper.eq(LogEntity::getModuleType, module);
        }

        if (StringUtils.isNotBlank(result)) {
            wrapper.eq(LogEntity::getResult, result);
        }

        if (StringUtils.isNotBlank(startDate)) {
            wrapper.ge(LogEntity::getCreateTime, startDate);
        }

        if (StringUtils.isNotBlank(endDate)) {
            wrapper.le(LogEntity::getCreateTime, endDate);
        }

        Page<Object> page = PageHelper.startPage(pageNo, pageSize);
        List<LogEntity> list = this.baseMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(list)) {
            return new PageInfo<>(new ArrayList<>());
        }

        List<LogRespVo> collect = list.stream().map(item -> {
            LogRespVo respVo = new LogRespVo();
            BeanUtils.copyProperties(item, respVo);
            return respVo;
        }).collect(Collectors.toList());

        for (LogRespVo respVo : collect) {
            respVo.setOperationType(handleOperationType(respVo.getOperationType()));
            respVo.setModuleType(handleModuleType(respVo.getModuleType()));
        }

        PageInfo<LogRespVo> pageInfo = new PageInfo<>(collect);
        pageInfo.setTotal(page.getTotal());
        return pageInfo;
    }

    private String handleModuleType(String moduleType) {
        String res ;
        switch (moduleType) {
            case "article":
                res = "文章";
                break;
            case "auth":
                res = "用户认证";
                break;
            case "oss":
                res = "阿里云OSS";
                break;
            case "email":
                res = "邮件";
                break;
            case "sms":
                res = "短信";
                break;
            case "system":
                res = "系统";
                break;
            case "user":
                res = "用户";
                break;
            default:
                res = "";
        }
        return res;
    }

    private String handleOperationType(String operationType) {
        String res;
        switch (operationType) {
            case "query":
                res = "查询";
                break;
            case "insert":
                res = "新增";
                break;
            case "edit":
                res = "编辑";
                break;
            case "delete":
                res = "删除";
                break;
            case "upload":
                res = "上传";
                break;
            case "download":
                res = "下载";
                break;
            case "login":
                res = "登录";
                break;
            case "register":
                res = "注册";
                break;
            case "export":
                res = "导出";
                break;
            case "import":
                res = "导入";
                break;
            default:
                res = "";
        }
        return res;
    }
}
